package com.uniuya;

import com.uniuya.excel.ExcelData;
import com.uniuya.excel.ExportExcelUtils;
import com.uniuya.util.MyUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainConvertor {
    public static final String KUAN_GAO ="\\d{1,2}a?\\d{1,2}a?";
    public static final String FLOOR_PATTEN="\\d{1,2}F:";

    public static void main(String[] args) throws Exception {

        //todo  注意！！！文件需要转成utf-8  去掉空格行
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\11\\55.txt"),"UTF8"));
        String line="";
        List<Map> res = new ArrayList<>();
        Map<String,List<String>> floorMap= new HashMap<>();
        List<String> allLines = new ArrayList<>();
        List<String> floors = new ArrayList<>();
        //当前楼层，为下面循环使用
        String floor = "0";
        while (!StringUtils.isEmpty(line=in.readLine())) {

            //判断楼层
//            if(line.contains("F:") || line.contains("屋顶")){
                if(line.contains("地下")){
                floor=line.replaceAll(":","");
                floor=floor.replaceAll("：","");
                floors.add(floor);

//                System.out.println(floor);
                floorMap.put(floor,new ArrayList<>());
            }
            //所有有效数据
            if(line.contains("----")){
                allLines.add(line);
                List<String> inFloors = floorMap.get(floor);
                inFloors.add(line);
            }
        }
        //把名字区分 以方便排序
        List<String> door = new ArrayList<>();
        List<String> window = new ArrayList<>();
        List<String> fmDoor = new ArrayList<>();
        List<String> fmWindow = new ArrayList<>();
        for(String one:allLines){
            String[] split = one.split("----");
            String name = split[0];
//            Integer num = Integer.parseInt(split[1].replaceAll("个",""));
            if(name.indexOf("FM")>=0){
                fmDoor.add(name);
            }else if(name.indexOf("FC")>=0){
                fmWindow.add(name);
            }else if(name.indexOf("M")>=0 && name.indexOf("C")>=0){
                if((name.indexOf("M")-name.indexOf("C")<0)){
                    door.add(name);
                }else {
                    window.add(name);
                }
            }else if(name.indexOf("M")>=0){
                door.add(name);
            }else if(name.indexOf("C")>=0){
                window.add(name);
            }
        }
        in.close();

        Map<String, Map<String, Integer>> stringMapMap = changeMap(floorMap, floors);
        Collections.sort(removeDuplicate(door));
        Collections.sort(removeDuplicate(window));
        Collections.sort(removeDuplicate(fmDoor));
        Collections.sort(removeDuplicate(fmWindow));
        ExcelData excelData = new ExcelData();
        List<String> titles = new ArrayList<>();
        titles.add("类别");
        titles.add("设计编号");
        titles.add("宽");
        titles.add("高");
        floors.stream().forEach(f->titles.add(f));
        titles.add("樘数");
        titles.add("参考图表");
        titles.add("备注");
        excelData.setTitles(titles);
        List<List<Object>> rows = new ArrayList<>();
        Integer tangshu = 0;
        for(String d:door){
            tangshu = 0;
            List<Object> list = new ArrayList<>();
            list.add("门");
            list.add(d);
            list.add(getKuan(d));
            list.add(getChang(d));
            for(String floor0:floors){
                Map<String, Integer> stringIntegerMap = stringMapMap.get(floor0);
                Integer integer = stringIntegerMap.get(d);
                if(null==integer || integer<=0){
                    list.add("");
                }else{
                    list.add(integer);
                    tangshu=tangshu+integer;
                }
            }
            list.add(tangshu);
            rows.add(list);
        }
        for(String d:fmDoor){
            tangshu = 0;
            List<Object> list = new ArrayList<>();
            list.add("防火门");
            list.add(d);
            list.add(getKuan(d));
            list.add(getChang(d));
            for(String floor0:floors){
                Map<String, Integer> stringIntegerMap = stringMapMap.get(floor0);
                Integer integer = stringIntegerMap.get(d);
                if(null==integer || integer<=0){
                    list.add("");
                }else{
                    list.add(integer);
                    tangshu=tangshu+integer;
                }
            }
            list.add(tangshu);
            rows.add(list);
        }
        for(String d:fmWindow){
            tangshu = 0;
            List<Object> list = new ArrayList<>();
            list.add("防火窗");
            list.add(d);
            list.add(getKuan(d));
            list.add(getChang(d));
            for(String floor0:floors){
                Map<String, Integer> stringIntegerMap = stringMapMap.get(floor0);
                Integer integer = stringIntegerMap.get(d);
                if(null==integer || integer<=0){
                    list.add("");
                }else{
                    list.add(integer);
                    tangshu=tangshu+integer;
                }
            }
            list.add(tangshu);
            rows.add(list);
        }
        for(String d:window){
            tangshu = 0;
            List<Object> list = new ArrayList<>();
            list.add("窗");
            list.add(d);
            list.add(getKuan(d));
            list.add(getChang(d));
            for(String floor0:floors){
                Map<String, Integer> stringIntegerMap = stringMapMap.get(floor0);
                Integer integer = stringIntegerMap.get(d);
                if(null==integer || integer<=0){
                    list.add("");
                }else{
                    list.add(integer);
                    tangshu=tangshu+integer;
                }
            }
            list.add(tangshu);
            rows.add(list);
        }

        excelData.setRows(rows);

        ExportExcelUtils.exportExcel(excelData);
//        allLines.stream().forEach(ss->System.out.println(ss));
        // 4.关闭相应的流
//        System.out.println(getChang("FC06a28乙"));
    }

    public static int getKuan(String name){
        Pattern p = Pattern.compile(KUAN_GAO);
        Matcher m = p.matcher(name);
        if (m.find()) {
            System.out.println("Found value0: " + m.group(0) );
            String res = m.group(0);
            String kuanStr = res.substring(0, 2);
            int kuan = Integer.parseInt(kuanStr) *100;
            if("a".equals(res.substring(2,3))){
                kuan=kuan+50;
            }
            return kuan;
        } else {
            return 0;
        }
    }
    public static int getChang(String name){
        Pattern p = Pattern.compile(KUAN_GAO);
        Matcher m = p.matcher(name);
        if (m.find()) {
            System.out.println("Found value0: " + m.group(0) );
            String res = m.group(0);
            String gaoStr = res.substring(2);
            if("a".equals(res.substring(2,3))){
                gaoStr = res.substring(3);
            }
            String gaoStr0 = gaoStr.substring(0, 2);
            int gao = Integer.parseInt(gaoStr0) *100;
            if("a".equals(gaoStr.substring(2))){
               gao=gao+50;
            }
            return gao;
        } else {
            return 0;
        }
    }

    public static Map<String,Map<String,Integer>> changeMap( Map<String,List<String>>  map,List<String> floors){
        Map<String,Map<String,Integer>> res= new HashMap<>();
        for(String floor:floors){
            Map<String,Integer> floorMap = new HashMap<>();
            List<String> infloors = map.get(floor);
            for(String one:infloors){
                String[] split = one.split("----");
                String name = split[0];
                Integer num = Integer.parseInt(split[1].replaceAll("个",""));
                floorMap.put(name,num);
            }
            res.put(floor,floorMap);
        }
        return res;
    }

    public   static   List  removeDuplicate(List list)  {
        for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {
            for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {
                if  (list.get(j).equals(list.get(i)))  {
                    list.remove(j);
                }
            }
        }
        return list;
    }
}
